package py.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import py.ColumnStrategy;
import py.SwitchConfig;
import py.filter.FilterCond;

public class ExcelConfig {
//	public static String parentPath = "";

	public static void init() {
		//读取配置文件
		Properties prop = new Properties();   
		String confName="config.xml";
		SwitchConfig.clearData();
		SAXReader reader = new SAXReader();
        try {   
        	File proFile=new File("ini.xml");
//        	InputStream in = Object.class.getResourceAsStream(proFile.getAbsolutePath());  
        	Document document = reader.read(proFile);
			Element configNameE = document.getRootElement(); 
//			Element configNameE=root.element("configName");
			confName =configNameE.getTextTrim();
//            confName = new String(prop.getProperty("configFile").getBytes("ISO-8859-1"), "utf-8").trim();   
            System.out.println("configFile Name is:"+confName);
        } catch (Exception e) {   
            e.printStackTrace();   
        } 
        
		SAXReader reader2 = new SAXReader();
		try {
//			String[] path=confName.split(File.separator);
			File conFile = new File(confName);
			//System.out.print(conFile.getAbsolutePath());
			Document document = reader2.read(conFile);
			Element root = document.getRootElement();
			Element excelCon = root.element("ExcelConfig");
			SwitchConfig.setTitleLine(Integer.parseInt(excelCon
					.attributeValue("TitleLine")));
			//迭代每个目标列
			List columnlst = excelCon.element("SwitchColumn")
					.elements("Column");
			for (Object o : columnlst) {
				ColumnStrategy cs = new ColumnStrategy();
				ArrayList sorColName = new ArrayList();
				Element columnE = (Element) o;
				cs.setDesColName(columnE.attributeValue("name"));
				//迭代每个源列
				List itemlst = columnE.elements("item");
				for (Object io : itemlst) {
					Element itemE = (Element) io;
					sorColName.add(itemE.getTextTrim());
				}
				cs.setSorColName(sorColName);
				//添加过滤
				Element filterE=columnE.element("filter");
				if(filterE!=null)
				{
					String filterType=filterE.attributeValue("filterType");
					String filterCon =filterE.attributeValue("filterCon");
					String filterValue=filterE.getText();
					//赋值
					FilterCond fc =new FilterCond();
					fc.setFilerType(filterType);
					fc.setFilterCon(filterCon);
					fc.setFilterValue(filterValue);
					cs.setFc(fc);	
				}
							
				SwitchConfig.getDesCols().add(cs);			
				
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

//	public static void resetParentPath() {
//		parentPath = "";
//	}
}
